package tmall.security.filter;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import tmall.framework.exception.ErrorEnum;
import tmall.framework.response.ResponseResult;
import tmall.security.dict.SecurityExceptionDict;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

 public class RestAccessDeniedHandler implements AuthenticationEntryPoint {

     @Override
     public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
         ErrorEnum tokenInfo = SecurityExceptionDict.TOKEN_UN_VALID;
         //String text = JSON
         response.setContentType("application/json");
         response.setCharacterEncoding("UTF-8");
         ResponseResult result = ResponseResult.failed(tokenInfo.getMsg(),tokenInfo.getCode());
         response.getWriter().write(result.toString());
     }
 }
